﻿//	Copyright (c) 2012 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ordinaire.Security.Data;

namespace Ordinaire.Security
{
    /// <summary>
    /// Provides administrator's rights. Use Ordinaire.Security.User.AsAdministrator() to create one.
    /// </summary>
    public class Administrator : User, IAdministrator
    {
        /// <summary>
        /// Returns new instance of Ordinaire.Security.Administrator.
        /// </summary>
        /// <param name="authentication">IAuthentication instance</param>
        internal Administrator(User user)
        {
            base.Id        = user.Id;
            base.Name      = user.Name;
            roles          = user.Roles;
            authentication = user.Authentication;
            authorisation  = user.Authorisation;
        }

        /// <summary>
        /// Assigns the username with the specified role name.
        /// </summary>
        /// <param name="username">username</param>
        /// <param name="name">role name</param>
        /// <returns>true if successful, else false</returns>
        public bool AssignRole(string username, string name)
        {
            Role role = Authorisation.GetRoleByName(name);
            if (role == null)
            {
                throw ExceptionFactory.Create<ArgumentException>(ExceptionMessage.InvalidRoleName, name);
            }

            return Authorisation.AssignRole(username, role, this);
        }

        /// <summary>
        /// Creates new user with the specified username and password
        /// </summary>
        /// <param name="username">the user's id</param>
        /// <param name="password">the user's password</param>
        /// <returns>true if user details that was created, else false</returns>
        public bool CreateUser(string username, string password)
        {
            UserData newUserData = Authentication.NewUser(username, password, this);
            return (newUserData == null) ? false : true;
        }

        /// <summary>
        /// Creates new role with the specified name. New role created  will be under admin role.
        /// </summary>
        /// <param name="name">the new role name</param>
        /// <returns>new instance of Ordinaire.Security.Role</returns>
        public Role CreateRole(string name)
        {
            return Authorisation.CreateRole(name, this);
        }
    }
}
